package com.itcode.controller;

import com.itcode.pojo.Result;
import com.itcode.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUpdateController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        final String originalFilename = file.getOriginalFilename();
        //UUID 重命名文件
        //文件后缀名
        final String extend = originalFilename.substring(originalFilename.lastIndexOf('.'));
        String filename = UUID.randomUUID().toString() + extend;
        //存储到本地
        //file.transferTo(new File("C:\\Users\\Thinkpad\\Desktop\\Files\\" + filename));

        final String url = AliOssUtil.uploadFile(filename, file.getInputStream());
        return Result.success(url);
    }
}
